package ua.in.iticket.addressbook.storage;

import ua.in.iticket.addressbook.person.Address;
import ua.in.iticket.addressbook.person.Person;
import ua.in.iticket.addressbook.person.PhoneNumber;

/**
 * Created by ivanov-av
 * 13.02.14 10:23.
 */
public class PersonCoder {
    public static String personEncode(Person person) {
        String line = "";
        line = new StringBuilder().append(line).append(FileStorageConfig.FULLNAME).append(FileStorageConfig.DELIMITER).append(person.getFullName()).append(System.lineSeparator()).toString();
        line = new StringBuilder().append(line).append(FileStorageConfig.AGE).append(FileStorageConfig.DELIMITER).append(person.getAge()).append(System.lineSeparator()).toString();
        line = new StringBuilder().append(line).append(FileStorageConfig.ADDRESS).append(FileStorageConfig.DELIMITER).append(person.getAddress().getCountry()).toString();
        line = new StringBuilder().append(line).append(FileStorageConfig.ADDRESS_DELIMITER).append(person.getAddress().getCity()).toString();
        line = new StringBuilder().append(line).append(FileStorageConfig.ADDRESS_DELIMITER).append(person.getAddress().getCountry()).append(System.lineSeparator()).toString();
        for (int i = 0; i < person.getPhoneNumbersSize(); i++) {
            String countryCode = person.getPhoneNumber(i).getCountryCode();
            String operatorCode = person.getPhoneNumber(i).getOperatorCode();
            String number = person.getPhoneNumber(i).getNumber();
            String note = person.getPhoneNumber(i).getNote();
            line = new StringBuilder().append(line).append(FileStorageConfig.PHONE).append(FileStorageConfig.DELIMITER).append(countryCode).append(FileStorageConfig.PHONE_DELIMITER).append(operatorCode).append(FileStorageConfig.PHONE_DELIMITER).append(number).append(FileStorageConfig.PHONE_DELIMITER).append(note).append(System.lineSeparator()).toString();
        }
        line = new StringBuilder().append(line).append(System.lineSeparator()).toString();
        return line;
    }

    public static Person personDecode(String[] strings) {
        Person person = new Person();
        for (int i = 0; i < strings.length; i++) {
            String[] array = strings[i].split(FileStorageConfig.DELIMITER);
            if ((null != strings[i]) && (array.length == 2)) {
                if (FileStorageConfig.FULLNAME.equals(array[0])) person.setFullName(array[1]);
                if (FileStorageConfig.AGE.equals(array[0])) person.setAge(Integer.parseInt(array[1]));
                if (FileStorageConfig.ADDRESS.equals(array[0])) {
                    String[] address = array[1].split(FileStorageConfig.ADDRESS_DELIMITER);
                    person.setAddress(new Address(address[0], address[1], address[2]));
                }
                if (FileStorageConfig.PHONE.equals(array[0])) {
                    String[] phone = array[1].split(FileStorageConfig.PHONE_DELIMITER);
                    if (phone.length == 1) person.addPhone(new PhoneNumber(phone[0], null, null, null));
                    if (phone.length == 2) person.addPhone(new PhoneNumber(phone[0], phone[1], null, null));
                    if (phone.length == 3) person.addPhone(new PhoneNumber(phone[0], phone[1], phone[2], null));
                    if (phone.length == 4) person.addPhone(new PhoneNumber(phone[0], phone[1], phone[2], phone[3]));
                }
            }
        }
        return person;
    }

    public static String[] addToBuffer (String[] buffer, String line) {
        String[] newBuffer = new String[buffer.length + 1];
        for (int i = 0; i < buffer.length; i++) {
            newBuffer[i] = buffer[i];
        }
        newBuffer[buffer.length] = line;
        return newBuffer;
    }
}

